<?php
require_once dirname(__FILE__) . '/inc.head.php';

$addrs = array();
if (isset($_SESSION['index' . USER])) {
  $cond_vals = new stdClass(); 
  $cond_vals->c = 't.customer_id = :v1';
  $cond_vals->v = array(':v1' => $_SESSION['index' . USER]->id);
  $addrs = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_customer_addr', $cond_vals,  't.first_name', 'ASC', NULL, NULL, NULL)->d;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/styles.css" />
<script src="../../../libs/jquery/jquery-1.9.0.min.js"></script>
<script type="text/javascript" src="js/sliding_effect.js"></script>
<style>
* {
  font-family: Trebuchet MS;
  font-size: 12px;
}
table.list {
  border-left: 1px solid #aaa;
  border-top: 1px solid #aaa;
  font-size: 13px;
  width: 100%;
}
table.list th.sort {
  text-align: left;
  height: 28px;
  background-color: #ddd;
  padding: 0 5px; 
  border-right: 1px solid #aaa;
  border-bottom: 1px solid #aaa;
  font-weight: bold;
  vertical-align: middle;
  cursor: pointer;
  color: #333;
}
table.list tr.rowodd td {
  padding: 0 5px;
  height: 25px;
  background-color: #eee;
  border-right: 1px solid #aaa;
  border-bottom: 1px solid #aaa;
  word-wrap: break-word;
  vertical-align: middle;
}
table.list tr.roweven td {
  padding: 0 5px;
  height: 25px;
  background-color: #fff;
  border-right: 1px solid #aaa;
  border-bottom: 1px solid #aaa;
  word-wrap: break-word;
  vertical-align: middle;
}
table.list tr.rowodd:hover td {
  background-color: #f4f3b2;
}
table.list tr.roweven:hover td {
  background-color: #f4f3b2;
}
table.list tr.none td {
  padding: 0 5px;
  height: 25px;
  background-color: #fff;
  border-right: 1px solid #666;
  border-bottom: 1px solid #666;
  word-wrap: break-word;
}
table.form {
  background-color: #fff;
  border-top: 1px solid #aaa;
  border-left: 1px solid #aaa;
  width: 100%;
}
table.form td {
  padding: 3px 5px; 
  vertical-align: top;
}
table.form td.title {
  background-color: #ddd !important;
  border-bottom: 1px solid #aaa;
  border-right: 1px solid #aaa;
  width: 150px;
}
table.form td.content {
  padding: 0;
  border-bottom: 1px solid #aaa;
  border-right: 1px solid #aaa;
}
table.form td.content input {
  width: 98%;
  height: 20px;
  border: 0;
  padding-left: 2px;
}
table.lo {
  width: 100%;
}
table.lo td {
  vertical-align: top;
}
.imgbtn {
  background-image: url(img/add.png);
  background-repeat: no-repeat;
  background-position: 5px 2px;
  padding: 5px 5px 5px 30px;
  border: 1px solid #666;
  background-color: #fff;
  cursor: pointer;
  border-radius: 3px;
  font-family: Lucida Sans Unicode;
}
.imgbtn:hover {
  background-color: #eee;
}
.ok {
  background-image: url(img/ok.png);
  width: 80px;
  height: 30px;
}
.cancel {
  background-image: url(img/cancel.png);
  width: 80px;
  height: 30px;
}
.img {
  background:url(img/delete24.png) no-repeat;
  cursor:pointer;
  border: none;
  width: 100%;
  height: 24px;
}
.edit {
  background:url(img/edit.png) no-repeat !important;
}
form {
  padding: 0;
  margin: 0;
}
</style>
</head>
<body>
<table class="lo" cellpadding=0 cellspacing=0>
  <tr>
    <td style="width: 200px;"><?php include dirname(__FILE__) . '/inc.left.php'?></td>
    <td style="padding-left: 20px; padding-top: 18px;">
      <input type='submit' value="Add New" class="imgbtn" onclick="save_addr('-1');"></input>
      <div>&nbsp;</div>
      <table class="list" cellpadding=0 cellspacing=0>
        <tr>
          <th class="sort" width="45px">Default</th>
          <th class="sort" width="100px">First Name</th>
          <th class="sort" width="100px">Last Name</th>
          <th class="sort">Address</th>
          <th class="sort" width="60px">City</th>
          <th class="sort" width="20px" style="border-right: none;padding-right: 0;">&nbsp;</th>
          <th class="sort" width="30px" style="padding-left: 0;">&nbsp;</th>
        </tr>
        <?php foreach ($addrs as $idx => &$addr) { ?>
        <tr class="<?php echo (($idx % 2 == 0) ? 'roweven' : 'rowodd'); ?>">
          <td><input type="radio" name="addr" <?php if ($addr->is_default == 1) echo 'checked="checked"'; ?> value="<?php echo $idx; ?>" onclick="set_new_addr('none');"></input></td>
          <td><?php echo MiscUtils::getShortName($addr->first_name, 20); ?>&nbsp;</td>
          <td><?php echo MiscUtils::getShortName($addr->last_name, 20); ?>&nbsp;</td>
          <td><?php echo MiscUtils::getShortName($addr->address1, 50); ?>&nbsp;</td>
          <td><?php echo MiscUtils::getShortName($addr->city, 20); ?>&nbsp;</td>
          <td style="border-right: none;padding-right: 0;">
            <input type='submit' value="" class="img edit" onclick="save_addr('<?php echo $idx; ?>');"></input>
          </td>
          <td style="padding-left: 0;">
            <form id="del_addr<?php echo $idx; ?>" action="<?php echo basename(__FILE__, '.php'); ?>.server.php" method="post">
              <input name="action" value="remove_addr" style="display: none;"></input>
              <input name="id" value="<?php echo $addr->id; ?>" style="display: none;"></input>
              <input name="is_default" value="<?php echo $addr->is_default; ?>" style="display: none;"></input>
              <input name="customer_id" value="<?php echo $addr->customer_id; ?>" style="display: none;"></input>
              <input name="to" value="<?php echo MiscUtils::curPageURL(); ?>" style="display: none;"></input>
              <input type='submit' value="" class="img"></input>
            </form>
            <script>
            $('#del_addr<?php echo $idx; ?>').submit(function() {
              return window.confirm('Are You Sure?');
            });
            </script>
          </td>
        </tr>
        <?php } ?>
        <?php if (count($addrs) == 0) { ?>
        <tr class="none"><td colspan=7>None</td></tr>
        <?php } ?>
      </table>
    </td>
  </tr>
</table>
<!-- Popup box for editing address -->
<div id="dia_addr" style="display: none;">
  <form action="<?php echo basename(__FILE__, '.php'); ?>.server.php" method="post">
    <input name="action" value="save_addr" style="display: none;"></input>
    <input name="to" value="<?php echo MiscUtils::curPageURL(); ?>" style="display: none;"></input>
    <input id="idx" name="idx" <?php if (isset($_SESSION['addr_idx'])) echo 'value="' . $_SESSION['addr_idx'] . '"'; ?> style="display: none;"></input>
    <input id="id" name="id" <?php if (isset($_SESSION['addr_id'])) echo 'value="' . $_SESSION['addr_id'] . '"'; ?> style="display: none;"></input>
    <table class="form" cellpadding=0 cellspacing=0>
      <tr>
        <td class="title">First Name</td>
        <td class="content"><input id="first_name" name="first_name" type="text" <?php if (isset($_SESSION['first_name'])) echo 'value="' . $_SESSION['first_name'] . '"'; ?>></input><?php if (isset($_SESSION['first_name_err'])) { ?><div style="color:red;"><?php echo $_SESSION['first_name_err']; ?></div><?php } ?></td>
      </tr>
      <tr>
        <td class="title">Last Name</td>
        <td class="content"><input id="last_name" name="last_name" type="text" <?php if (isset($_SESSION['last_name'])) echo 'value="' . $_SESSION['last_name'] . '"'; ?>></input><?php if (isset($_SESSION['last_name_err'])) { ?><div style="color:red;"><?php echo $_SESSION['last_name_err']; ?></div><?php } ?></td>
      </tr>
      <tr>
        <td class="title">Address1</td>
        <td class="content"><input id="address1" name="address1" type="text" <?php if (isset($_SESSION['address1'])) echo 'value="' . $_SESSION['address1'] . '"'; ?>></input><?php if (isset($_SESSION['address1_err'])) { ?><div style="color:red;"><?php echo $_SESSION['address1_err']; ?></div><?php } ?></td>
      </tr>
      <tr>
        <td class="title">Address2 (optional)</td>
        <td class="content"><input id="address2" name="address2" type="text" <?php if (isset($_SESSION['address2'])) echo 'value="' . $_SESSION['address2'] . '"'; ?>></input><?php if (isset($_SESSION['address2_err'])) { ?><div style="color:red;"><?php echo $_SESSION['address2_err']; ?></div><?php } ?></td>
      </tr>
      <tr>
        <td class="title">City</td>
        <td class="content"><input id="city" name="city" type="text" <?php if (isset($_SESSION['city'])) echo 'value="' . $_SESSION['city'] . '"'; ?>></input><?php if (isset($_SESSION['city_err'])) { ?><div style="color:red;"><?php echo $_SESSION['city_err']; ?></div><?php } ?></td>
      </tr>
      <tr>
        <td class="title">Country</td>
        <td class="content"><input id="country" name="country" type="text" <?php if (isset($_SESSION['country'])) echo 'value="' . $_SESSION['country'] . '"'; ?>></input><?php if (isset($_SESSION['country_err'])) { ?><div style="color:red;"><?php echo $_SESSION['country_err']; ?></div><?php } ?></td>
      </tr>
      <tr>
        <td class="title">Postal Code</td>
        <td class="content"><input id="zip" name="zip" type="text" <?php if (isset($_SESSION['zip'])) echo 'value="' . $_SESSION['zip'] . '"'; ?>></input><?php if (isset($_SESSION['zip_err'])) { ?><div style="color:red;"><?php echo $_SESSION['zip_err']; ?></div><?php } ?></td>
      </tr>
      <tr>
        <td class="title">Phone</td>
        <td class="content"><input id="phone" name="phone" type="text" <?php if (isset($_SESSION['phone'])) echo 'value="' . $_SESSION['phone'] . '"'; ?>></input><?php if (isset($_SESSION['phone_err'])) { ?><div style="color:red;"><?php echo $_SESSION['phone_err']; ?></div><?php } ?></td>
      </tr>
      <tr>
        <td class="title">Email</td>
        <td class="content"><input id="email" name="email" type="text" <?php if (isset($_SESSION['email'])) echo 'value="' . $_SESSION['email'] . '"'; ?>></input><?php if (isset($_SESSION['email_err'])) { ?><div style="color:red;"><?php echo $_SESSION['email_err']; ?></div><?php } ?></td>
      </tr>
    </table>
    <div style="text-align: center; margin-top: 5px;">
      <input class="imgbtn ok" type="submit" value="OK"></input>
      <input class="imgbtn cancel" type="submit" value="Cancel" onclick="event.returnValue = false;if(event.preventDefault) event.preventDefault();$('#dia_addr').dialog('close');"></input>
    </div>
  </form>
</div>
<script src="../../../libs/jquery/jquery-1.9.0.min.js"></script>
<script type="text/javascript" src="../../../incls/js/myutils.js"></script>
<script type="text/javascript" src="../../../libs/json/json2.js"></script>
<script src="../../../libs/jquery_ui/js/jquery-ui-1.10.2.custom.js"></script>
<link href="jquery_ui/css/smoothness/jquery-ui-1.10.2.custom.css" rel="stylesheet">
<script>
var _addrs = JSON.parse('<?php echo addslashes(json_encode($addrs)); ?>');
$(function(){
  <?php if (isset($_SESSION['addr_idx'])) { ?>
  $('#dia_addr').dialog({
    title: 'Edit Address',
    width: 700
  });
  <?php } ?>
});

function save_addr(idx) {
  if (idx != -1) {
    var addr = _addrs[idx];
    $('#idx')[0].value = idx;
    $('#id')[0].value = addr.id;
    $('#first_name')[0].value = addr.first_name;
    $('#last_name')[0].value = addr.last_name;
    $('#address1')[0].value = addr.address1;
    $('#address2')[0].value = addr.address2;
    $('#city')[0].value = addr.city;
    $('#country')[0].value = addr.country;
    $('#zip')[0].value = addr.zip;
    $('#phone')[0].value = addr.phone;
    $('#email')[0].value = addr.email;
  } else {
    $('#idx')[0].value = idx;
    $('#id')[0].value = 0;
    $('#first_name')[0].value = '<?php echo $_SESSION['index' . USER]->first_name; ?>';
    $('#last_name')[0].value = '<?php echo $_SESSION['index' . USER]->last_name; ?>';
    $('#address1')[0].value = '';
    $('#address2')[0].value = '';
    $('#city')[0].value = '';
    $('#country')[0].value = '';
    $('#zip')[0].value = '';
    $('#phone')[0].value = '<?php echo $_SESSION['index' . USER]->phone; ?>';;
    $('#email')[0].value = '<?php echo $_SESSION['index' . USER]->account; ?>';;
  }
  $('#dia_addr').dialog({
    title: 'Edit Address',
    width: 700
  });
}
</script>
</body>
</html>